<?php
/**
 * Application level Controller
 *
 * This file is application-wide controller file. You can put all
 * application-wide controller-related methods here.
 *
 * PHP versions 4 and 5
 *
 * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
 * Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
 *
 * Licensed under The MIT License
 * Redistributions of files must retain the above copyright notice.
 *
 * @copyright     Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
 * @link          http://cakephp.org CakePHP(tm) Project
 * @package       cake
 * @subpackage    cake.app
 * @since         CakePHP(tm) v 0.2.9
 * @license       MIT License (http://www.opensource.org/licenses/mit-license.php)
 */

/**
 * Application Controller
 *
 * Add your application-wide methods in the class below, your controllers
 * will inherit them.
 *
 * @package       cake
 * @subpackage    cake.app
 */
class AppController extends Controller {
    var $components = array('Cookie', 'Session');
    var $member = array();
    var $permissions = array();
    var $errorCode = -1;
    var $pageStyles = array();
    var $routers = array();
    function input($v)
    {
        return mysql_real_escape_string($v);
    }
    
    function checkLogin() {
        if(isset($this->member) && !empty($this->member)){
            return true;
        }else{
            return false;
        }
    }
    function beforeFilter() {
        $_user = $this->Session->read('user');
        if(!empty($_user)) {
            $this->member = $_user;
            return true;
        } else {
            $this->member = array();
            return false;
        }
        parent::beforeFilter();
    }
    function afterFilter() {
        parent::afterFilter();
    }
    
    function beforeRender() {
        $this->set('member', $this->member);
        $this->set('errorCode', $this->errorCode);
        $this->set('pageStyles', $this->pageStyles);
        $this->set('routers', array(
            'controller' => $this->params['controller'],
            'action' => $this->params['action']
        ));
        parent::beforeRender();
    }
    
    function needPermissions($need_perm) {
        if(!$this->checkLogin()) {
            $this->redirect(array('controller'=>'users', 'action'=>'login'));
        }
        if(!in_array($need_perm, $this->member['permissions'])) {
            exit('Access Denied');
        }
    }
}
